<template>
  <div>
    <el-dialog :visible="showDialog" v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="close"
      :title="form.id ? '修改标签' : '新增标签'">
      <el-form ref="formRef" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="所属学科" prop="Discipline">
          <el-select v-model="form.subjectID" placeholder="请选择下拉选择所属学科" clearable :style="{ width: '100%' }">
            <el-option v-for="(item, index) in list" :key="index" :label="item.label" :value="item.value"
              :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="name">
          <el-input v-model="form.tagName" placeholder="请输入目录名称" clearable :style="{ width: '100%' }"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { add, update } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  name: 'TagsAdd',
  inheritAttrs: false,
  components: {},
  props: {
    showDialog: {
      typeof: Boolean,
      default: false
    },
    addto: {
      typeof: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      form: {
        subjectID: undefined,
        tagName: '',
      },
      rules: {
        subjectID: [{
          required: true,
          message: '请选择下拉选择所属学科',
          trigger: 'change'
        }],
        tagName: [{
          required: true,
          message: '请输入目录名称',
          trigger: 'blur'
        }],
      },
      list: [],
    }
  },
  computed: {},
  watch: {},
  created () {

  },
  watch: {
    async showDialog (val) {
      // console.log(val)
      if (!val) {
        this.form.id = ""
        this.close()
        console.log('showDialog')
        return
      }
      if (this.addto.id) {
        this.form.subjectID = this.addto.subjectID
        this.form.tagName = this.addto.tagName
      }
      try {
        const { data } = await simple()
        this.list = data
      } catch (error) {
        console.log(error)
      }
    }
  },
  mounted () { },
  methods: {
    Fnet (row) {
      this.form = row
      console.log(this.form)
    },

    async onSubjects () {
      const res = await simple({})
      console.log(res)
    },
    onOpen () {
    },
    resetForm () {
      this.$refs['formRef'].resetFields();
    },
    // 关闭弹窗
    close () {
      this.$refs['formRef'].resetFields()
      this.$emit('update:showDialog', false)
    },
    handelConfirm () {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) {
          return
        }
        // 发送请求
        // 如果有id则为修改标签  数据回显   没有则新增
        if (this.form.id) {
          // 修改标签
          await update({
            subjectID: this.form.subjectID,
            tagName: this.form.tagName,
            id: this.form.id
          })
        } else {
          // 新增标签
          console.log('新增')
          await add(this.form)
        }
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        this.$emit('success')
        this.$refs['formRef'].resetFields()
        this.close()
      })
    },
  }
}

</script>
<style>

</style>
